﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.APP.Classes;
using _WebShared.Classes;
using System.IO;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PS_Employees : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private IEmployeeTypeBLL et = new EmployeeTypeBLL();
        private IEmployeeStatusBLL statusBLL = new EmployeeStatusBLL();
        private IJobSiteBLL job = new JobSiteBLL();
        private IPositionBLL posBLL = new PositionBLL();
        private IClassBLL classBLL = new ClassBLL();
        Main main = new Main();

        private EmployeeInfo empInfo;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadEmpTypeSeq();
                LoadEmpStatusSeq();

                rblEmpTypeSeq.SelectedIndex = 0;
                RadTabStrip1.SelectedIndex = -1;
                LoadGrid();
            }            
        }

        private void LoadGrid()
        {
            //Nullable<int> EmpTypeSeq = rblEmpTypeSeq.SelectedValue.Length > 0 ? Int32.Parse(rblEmpTypeSeq.SelectedValue) : (int?)null;
            IList<EmployeeInfo> infoList = empBLL.ListEmployeeByTypes(rblEmpTypeSeq.SelectedValue, "", "");
            //List<EmployeeInfo> list = new List<EmployeeInfo>();

            //for (int i = 0; i < infoList.Count; i++)
            //{
            //    EmployeeInfo emp = new EmployeeInfo();
            //    emp.Sequence = infoList[i].Sequence;
            //    emp.Person.FirstName = infoList[i].Person.FirstName + infoList[i].Person.LastName;
            //    emp.WorkingJobSite.Name = infoList[i].WorkingJobSite.Name;

            //    list.Add(emp);
            //}
            RadGrid1.DataSource = infoList;// emp.SelectGrid(strEmpTypeSeq, strEmpStatusSeq, "1", "working_job_site_ref, pos_ref");

            GridGroupByExpression g = new GridGroupByExpression(RadGrid1.Columns[4]);
            RadGrid1.MasterTableView.GroupByExpressions.Add(g);
            RadGrid1.DataBind();

        }

        private void LoadEmpTypeSeq()
        {
            IList<EmployeeTypeInfo> infoList = et.ListEmployeeTypes(null);

            rblEmpTypeSeq.DataSource = infoList;
            rblEmpTypeSeq.DataBind();
        }

        private void LoadEmpStatusSeq()
        {
            IList<EmployeeStatusInfo> infoList = statusBLL.LoadListEmployeeStatus();

            ddlEmpStatusSeq.DataSource = infoList;
            ddlEmpStatusSeq.DataBind();
        }

        protected void CheckDateValid(object source, ServerValidateEventArgs args)
        {
            if (!DateTimeX.IsValidDmyDate(args.Value, true))
            {
                args.IsValid = false;
            }
        }

        private void AddPageView(RadTab Tab)
        {
            RadPageView pageView = new RadPageView();
            pageView.ID = Tab.Value;
            
            RadMultiPage1.PageViews.Add(pageView);
            Tab.PageViewID = pageView.ID;
        }

        protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)
        {
            AddPageView(e.Tab);
            e.Tab.PageView.Selected = true;
        }

        protected void RadMultiPage1_PageViewCreated(object sender, RadMultiPageEventArgs e)
        {
            string strUserControlName = "../UserControls/" + e.PageView.ID + ".ascx";

            Control userControl = Page.LoadControl(strUserControlName);
            userControl.ID = e.PageView.ID + "_userControl";

            e.PageView.Controls.Add(userControl);
        }

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "RowClick" && e.Item is GridDataItem)
            {
                e.Item.Selected = true;
                PanelDetail.Enabled = (RadGrid1.SelectedItems.Count > 0);
                Session["emp_seq"] = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"].ToString();

                IList<EmployeeInfo> empList = empBLL.ListEmployees(ssMng.EmpSeq);
                if (empList.Count > 0)
                {
                    empInfo = empList[0];

                    //IList<PositionInfo> posList = posBLL.ListPositions(empInfo.Position);
                    //empInfo = emp.GetEmployeeInfo(ssMng.EmpSeq);
                    //posInfo = pos.GetPositionInfo(empInfo.PositionSeq);
                    //IList<ClassInfo> classInfo = _class.ListClasses(empInfo.ClassSeq, null);

                    lblOfficialID.Text = empInfo.Person.Official;
                    lblEmpTypeName.Text = empInfo.EmployeeType.Name;// .EmpTypeName;
                    lblEmpName.Text = empInfo.Person.FirstName + " " + empInfo.Person.LastName;
                    lblEmpNameEng.Text = empInfo.Person.FirstNameEng + " " + empInfo.Person.LastNameEng;
                    lblPosRef.Text = empInfo.Position.RefNo;
                    lblPosName.Text = empInfo.Position.Name;
                    lblClassName.Text = empInfo.Class.Name;
                    lblJobsiteName.Text = empInfo.Position.JobSite.Name;
                    lblWorkingJobsiteName.Text = empInfo.WorkingJobSite.Name;
                    lblSalary.Text = empInfo.Salary.ToString();
                    lblEmpStatusName.Text = empInfo.Status.Name;

                    if (File.Exists(Server.MapPath("../../OtherImages/Person/" + empInfo.Sequence.ToString() + ".jpg").Replace("\\", "/")))
                        imgEmp.ImageUrl = "../../OtherImages/Person/" + empInfo.Sequence.ToString() + ".jpg";
                    else
                        imgEmp.ImageUrl = "~/App_UI/Images/Buttons/No Picture.jpg";
                }
                if (RadMultiPage1.SelectedIndex >= 0)
                {
                    RadMultiPage1.PageViews.Clear();
                    AddPageView(RadTabStrip1.SelectedTab);
                    RadMultiPage1.PageViews[0].Selected = true;
                }
            }
        }

        protected void cmdEditProfile_Click(object sender, ImageClickEventArgs e)
        {

        }
    }
}
